#include "TChartClrDialog.h"
#include "TChart.h"
#include <QStringListModel>
#include <QColorDialog>
#include "common.h"
#include <QSpinBox>


//-------------------------------------

TChartClrDialog::TChartClrDialog( QWidget * parent ) :
    QDialog( parent ),
    crtItems( 0 )
{
    resize( 410, 240 );

    glay = new QGridLayout( this );
    glay->setContentsMargins( 0, 0, 0, 0 );

    listView = new QListView( this );
    btnClr = new QPushButton( "Curve Color", this );
    cbLineType = new QComboBox( this );
    buttonBox = new QDialogButtonBox( this );
    sbLineWidth = new QSpinBox( this );
    sbSplineDots = new QSpinBox( this );
    lblSplineDots = new QLabel( "Spline Dots Num", this );
    lblLineType = new QLabel( "Line Type", this );
    lblLineWidth = new QLabel( "Line Width", this );
    btnDotsColor = new QPushButton( "Dots Color", this );
    sbDotsSize = new QSpinBox( this );
    lblDotsSize = new QLabel( "Dots Size", this );
    lblDotsType = new QLabel( "Dots Type", this );
    cbDotsType = new QComboBox( this );

    glay->addWidget( listView, 0, 0, 7, 1 );
    glay->addWidget( btnClr, 0, 1, 1, 2 );
    glay->addWidget( cbLineType, 3, 2, 1, 1 );
    glay->addWidget( buttonBox, 7, 0, 1, 3 );
    glay->addWidget( sbLineWidth, 2, 2, 1, 1 );
    glay->addWidget( sbSplineDots, 1, 2, 1, 1 );
    glay->addWidget( lblSplineDots, 1, 1, 1, 1 );
    glay->addWidget( lblLineType, 3, 1, 1, 1 );
    glay->addWidget( lblLineWidth, 2, 1, 1, 1 );
    glay->addWidget( btnDotsColor, 4, 1, 1, 2 );
    glay->addWidget( sbDotsSize, 5, 2, 1, 1 );
    glay->addWidget( lblDotsSize, 5, 1, 1, 1 );
    glay->addWidget( lblDotsType, 6, 1, 1, 1 );
    glay->addWidget( cbDotsType, 6, 2, 1, 1 );


    sbSplineDots->setMinimum( 10 );
    sbSplineDots->setMaximum( 999999900 );
    sbSplineDots->setSingleStep( 100 );

    buttonBox->setOrientation( Qt::Horizontal );
    buttonBox->setStandardButtons( QDialogButtonBox::Cancel | QDialogButtonBox::Ok );

    setWindowTitle( "Color Dialog" );

    cbLineType->addItem( "NoPen" );
    cbLineType->addItem( "SolidLine" );
    cbLineType->addItem( "DashLine" );
    cbLineType->addItem( "DotLine" );
    cbLineType->addItem( "DashDotLine" );
    cbLineType->addItem( "DashDotDotLine" );
    cbLineType->addItem( "CustomDashLine" );
    cbLineType->setEditable( false );

    cbDotsType->addItem( "NoSymbol" );
    cbDotsType->addItem( "Ellipse" );
    cbDotsType->addItem( "Rect" );
    cbDotsType->addItem( "Diamond" );
    cbDotsType->addItem( "Triangle" );
    cbDotsType->addItem( "DTriangle" );
    cbDotsType->addItem( "UTriangle" );
    cbDotsType->addItem( "LTriangle" );
    cbDotsType->addItem( "RTriangle" );
    cbDotsType->addItem( "Cross" );
    cbDotsType->addItem( "XCross" );
    cbDotsType->addItem( "HLine" );
    cbDotsType->addItem( "VLine" );
    cbDotsType->addItem( "Star1" );
    cbDotsType->addItem( "Star2" );
    cbDotsType->addItem( "Hexagon" );
    cbDotsType->setEditable( false );

    connect( buttonBox, SIGNAL(accepted()), this, SLOT(accept()) );
    connect( buttonBox, SIGNAL(rejected()), this, SLOT(reject()) );
    connect( listView, SIGNAL(clicked(QModelIndex)), this, SLOT(sltListviewClickItem(QModelIndex)) );
    connect( btnClr, SIGNAL(clicked()), this, SLOT(sltSetColor()) );
    connect( cbLineType, SIGNAL(currentIndexChanged(int)), this, SLOT(sltSetLineType()) );
    connect( sbLineWidth, SIGNAL(valueChanged(int)), this, SLOT(sltSetWidth()) );
    connect( sbSplineDots, SIGNAL(valueChanged(QString)), this, SLOT(sltSetSplineDotsNum()) );
    connect( btnDotsColor, SIGNAL(clicked()), this, SLOT(sltSetDotsColor()) );
    connect( cbDotsType, SIGNAL(currentIndexChanged(int)), this, SLOT(sltSetDotsType()) );
    connect( sbDotsSize, SIGNAL(valueChanged(int)), this, SLOT(sltSetDotsSize()) );
}

//-------------------------------------

TChartClrDialog::~TChartClrDialog()
{
    delete glay;
    delete listView;
    delete btnClr;
    delete cbLineType;
    delete buttonBox;
}

//-------------------------------------

int TChartClrDialog::Exec( QVector< TChartItem* > * ci )
{
    if( ci == 0 )
        return QDialog::Rejected;

    QStringListModel mdl;
    QStringList sl;

    crtItems = ci;

    for( int i=0; i < crtItems->count(); ++i )
        sl.append( crtItems->at( i )->Name );

    mdl.setStringList( sl );
    listView->setModel( &mdl );
    listView->selectionModel()->select( mdl.index( 0, 0 ), QItemSelectionModel::Select );
    sltListviewClickItem( mdl.index( 0, 0 ) );

    return exec();
}

//-------------------------------------

void TChartClrDialog::sltListviewClickItem( QModelIndex ind )
{
    TChartItem * chit = getTChartItem( ind );
    if( chit )
    {
        if( chit->Name == TCHART_BACKGROUND )
        {
            cbLineType->setEnabled( false );
            sbLineWidth->setEnabled( false );
            sbSplineDots->setEnabled( false );
            sbDotsSize->setEnabled( false );
            cbDotsType->setEnabled( false );
            btnDotsColor->setEnabled( false );
        }
        else if( chit->Name == TCHART_GRID )
        {
            cbLineType->setEnabled( true );
            sbLineWidth->setEnabled( true );
            sbSplineDots->setEnabled( false );
            sbDotsSize->setEnabled( false );
            cbDotsType->setEnabled( false );
            btnDotsColor->setEnabled( false );
        }
        else
        {
            cbLineType->setEnabled( true );
            sbLineWidth->setEnabled( true );
            sbSplineDots->setEnabled( true );
            sbDotsSize->setEnabled( true );
            cbDotsType->setEnabled( true );
            btnDotsColor->setEnabled( true );
        }
        btnClr->setPalette( QPalette( chit->Color ) );
        cbLineType->setCurrentIndex( chit->LineType );
        sbLineWidth->setValue( chit->Width );
        sbSplineDots->setValue( chit->SplineSize );
        sbDotsSize->setValue( chit->DotSize );
        cbDotsType->setCurrentIndex( chit->DotType+1 );
        btnDotsColor->setPalette( QPalette( chit->DotColor ) );
    }
}

//-------------------------------------

void TChartClrDialog::sltSetColor()
{
    TChartItem * chit = getSelectedTChartItem();
    if( chit )
    {
        QColor c = btnClr->palette().color( QPalette::Button );
        c = QColorDialog::getColor( c );
        chit->Color = c;
        btnClr->setPalette( QPalette( chit->Color ) );
    }
}

//-------------------------------------

void TChartClrDialog::sltSetLineType()
{
    TChartItem * chit = getSelectedTChartItem();
    if( chit )
    {
        chit->LineType = (Qt::PenStyle)cbLineType->currentIndex();
    }
}

//-------------------------------------

void TChartClrDialog::sltSetWidth()
{
    TChartItem * chit = getSelectedTChartItem();
    if( chit )
    {
        chit->Width = sbLineWidth->value();
    }
}

//-------------------------------------

void TChartClrDialog::sltSetSplineDotsNum()
{
    TChartItem * chit = getSelectedTChartItem();
    if( chit )
    {
        chit->SplineSize = sbSplineDots->value();
    }
}

//-------------------------------------

TChartItem * TChartClrDialog::getSelectedTChartItem()
{
    QModelIndexList il = listView->selectionModel()->selectedIndexes();
    if( il.count() )
        return getTChartItem( il.at( 0 ) );
    return 0;
}

//-------------------------------------

TChartItem * TChartClrDialog::getTChartItem( QModelIndex ind )
{
    TChartItem * chit = 0;

    QString chartItemName = listView->model()->data( ind ).toString();
    for( QVector< TChartItem* >::iterator it = crtItems->begin(); it != crtItems->end(); ++it )
    {
        if( (*it)->Name == chartItemName )
        {
            chit = *it;
            break;
        }
    }

    return chit;
}

//-------------------------------------

void TChartClrDialog::sltSetDotsColor()
{
    TChartItem * chit = getSelectedTChartItem();
    if( chit )
    {
        QColor c = btnDotsColor->palette().color( QPalette::Button );
        c = QColorDialog::getColor( c );
        chit->DotColor = c;
        btnDotsColor->setPalette( QPalette( chit->DotColor ) );
    }
}

//-------------------------------------

void TChartClrDialog::sltSetDotsType()
{
    TChartItem * chit = getSelectedTChartItem();
    if( chit )
    {
        chit->DotType = (QwtSymbol::Style)( cbDotsType->currentIndex()-1 );
    }
}

//-------------------------------------

void TChartClrDialog::sltSetDotsSize()
{
    TChartItem * chit = getSelectedTChartItem();
    if( chit )
    {
        chit->DotSize = sbDotsSize->value();
    }
}

//-------------------------------------









